package com.abel.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.abel.domain.DesiredPosition;
import com.abel.domain.PersonProfile;
import com.abel.domain.types.PayMode;
import com.abel.manager.PersonProfileManager;
import com.abel.util.UserSessionLocator;
import com.abel.web.view.DesiredPositionsViewBean;

public class DesiredPositionsController extends SimpleFormController{

	protected final Log logger = LogFactory.getLog(getClass());
	
	private PersonProfileManager personProfileManager;

	private String previousView = null;
	
	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		logger.debug("@DesiredPositionsController : onSubmit : entered");

		ModelAndView mav = null;
		
		Map<String, Object> modelMap = new HashMap<String, Object>();
		
		String action = request.getParameter("previous") != null ? "previous" : "continue";
		
		DesiredPositionsViewBean viewBean = (DesiredPositionsViewBean)command;
		DesiredPosition desiredPosition = new DesiredPosition();
		desiredPosition.setPosition1stDesired(viewBean.getPosition1stDesired());
		desiredPosition.setPosition2ndDesired(viewBean.getPosition2ndDesired());
		//desiredPosition.setSalaryDesired(viewBean.getSalaryDesired() == "" ? 0 : Double.parseDouble(viewBean.getSalaryDesired()));
		//desiredPosition.setLeastSalaryAccepted(viewBean.getLeastSalaryAccepted() == "" ? 0 : Double.parseDouble(viewBean.getLeastSalaryAccepted()));
		desiredPosition.setSalaryDesired(viewBean.getSalaryDesired());
		desiredPosition.setLeastSalaryAccepted(viewBean.getLeastSalaryAccepted());
		desiredPosition.setSalaryMode(PayMode.getPayModeCode(viewBean.getSalaryDesiredMode()));
		desiredPosition.setLeastSalaryMode(PayMode.getPayModeCode(viewBean.getLeastSalaryMode()));
		
		try {
			personProfileManager.updateDesiredPositionInfo(desiredPosition);
			
		} catch (Exception e) {

			logger.error("@DesiredPositionsController : onSubmit : exception " + e);
			errors.rejectValue("", "error.null",null,"A system error occurred while registering your information. Please log out and log back in.");
			return showForm(request,response,errors);

		}
		if (action != null && action.equals("previous")) {
			mav = new ModelAndView("redirect:" + getPreviousView());
		}
		else {
			mav = new ModelAndView("redirect:" + getSuccessView());
		}
		mav.addAllObjects(modelMap);
		return mav;

	}



	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		
		//get DesiredPositions from threadlocal
		PersonProfile personProfile = UserSessionLocator.getUserSession().getPerson().getApplication().getPersonProfile();
		if (personProfile.getDesiredPosition() == null) {
			//entering info for the first time
			DesiredPositionsViewBean viewBean = new DesiredPositionsViewBean();
			viewBean.setSalaryDesiredMode("hourly");
			viewBean.setLeastSalaryMode("hourly");
			return viewBean;
		}
		else {
			//information already exists - returning user
			DesiredPosition desiredPosition = personProfile.getDesiredPosition();
			DesiredPositionsViewBean viewBean = new DesiredPositionsViewBean();
			
			viewBean.setPosition1stDesired(desiredPosition.getPosition1stDesired());
			viewBean.setPosition2ndDesired(desiredPosition.getPosition2ndDesired());
//			viewBean.setSalaryDesired(desiredPosition.getSalaryDesired() >= 0 ? Integer.toString((int)desiredPosition.getSalaryDesired())   : "");
//			viewBean.setLeastSalaryAccepted(desiredPosition.getLeastSalaryAccepted() >= 0 ? Integer.toString((int)desiredPosition.getLeastSalaryAccepted()) : "");
			viewBean.setSalaryDesired(desiredPosition.getSalaryDesired());
			viewBean.setLeastSalaryAccepted(desiredPosition.getLeastSalaryAccepted());
			viewBean.setSalaryDesiredMode(PayMode.getPayModeByCode(desiredPosition.getSalaryMode() != null && !desiredPosition.getSalaryMode().equals("") ? viewBean.getSalaryDesiredMode() : PayMode.annual.getPayModeCode()));
			viewBean.setLeastSalaryMode(PayMode.getPayModeByCode(desiredPosition.getLeastSalaryMode() != null && !desiredPosition.getLeastSalaryMode().equals("") ? viewBean.getLeastSalaryMode() : PayMode.annual.getPayModeCode()));
			return viewBean;
		}
	}



	/**
	 * @param personProfileManager the personProfileManager to set
	 */
	public void setPersonProfileManager(PersonProfileManager personProfileManager) {
		this.personProfileManager = personProfileManager;
	}



	/**
	 * @param previousView the previousView to set
	 */
	public void setPreviousView(String previousView) {
		this.previousView = previousView;
	}



	/**
	 * @return the previousView
	 */
	public String getPreviousView() {
		return previousView;
	}

}
